package com.cheyun.netsalev3.view

import android.content.Intent
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.databinding.ActivityStockSearchBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.viewmodel.StockSearchActivityViewModel
import com.cheyun.netsalev3.widget.FlowLayoutManager

class StockSearchActivity : BaseActivity<ActivityStockSearchBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_stock_search
    }

    override fun initViewModel(): ViewModel? {
        return ViewModelProviders.of(this).get(StockSearchActivityViewModel::class.java)
    }

    override fun initData() {
    }


    private lateinit var adapter: MyAdapter

    override fun initView() {
        binding.recyclerView.layoutManager = FlowLayoutManager()
        adapter = MyAdapter(R.layout.stock_search_item, binding.viewModel)
        binding.recyclerView.adapter = adapter
        subscribeUi()
    }

    private fun subscribeUi() {
        binding.viewModel?.listItem?.observe(this, Observer { lists ->
            Log.e("subscribeUi", lists.toString())
            if (lists != null) {
                adapter.submitList(lists)
            }
        })
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode==102){
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                binding.viewModel?.setCarData(carBrandParam)
            }
            subscribeUi()
        }
    }
}
